var params = {
  typeid: null,
  page: 1
}

window.onload = function () {
  $('.navigation li').eq(2).addClass('active')
}

$(document).on('click', '.js_menu_list', function () {
  params.typeid = $(this).attr('data-typeid')
  $(this).addClass('active').siblings().removeClass('active')
  getNews(parseInt(params.typeid), 1)
})

function getMenu() {
  $.ajax({
    type: "GET",
    url: "http://221.10.2.222:5680/api/ow/NewsType/TypeList/1",
    dataType: "json",
    cache: false,
    success: function (res) {
      console.log(res)
      var data = {
        list: res
      }
      var html = template('menu_tpl', data);
      document.getElementById('menu_html').innerHTML = html;
      $('.js_menu_list').eq(0).addClass('active')
      params.typeid = res[0].id
      getNews(params.typeid, 1)
    },
    error: function (e) {}
  })
}

function getNews(type, page) {
  $.ajax({
    url: "http://221.10.2.222:5680/api/ow/news/list",
    type: "GET",
    dataType: "json",
    data: {
      type: type,
      page: page,
      keyword: ' ',
      pagesize: 17
    },
    success: function (res) {
      console.log(res)
      res.resultdata = JSON.parse(res.resultdata)
      var html = template('news_tpl', res);
      document.getElementById('news_html').innerHTML = html;
      $('img').error(function () {
        var imgurl = params.typeid == 1 ? '/src/images/news.jpg' : '/src/images/timg.jpg'
        $(this).attr('src', imgurl);
     });
      laypage({
        cont: 'page1',
        pages: Math.ceil(res.count / 17),
        curr: page || 1, //当前页
        first: false,
        last: false,
        prev: '<',
        next: '>',
        jump: function (obj, first) {
          window.scrollTo(0, 300)
          if (!first) {
            getNews(params.typeid, obj.curr);
          }
        }
      });
    },
    error: function (e) {
      console.log(JSON.stringify(e))
    }
  })
}
getMenu()